from datetime import datetime

from sqlalchemy.orm import relationship

from exts import db
from server.resource.util.Serializable import Serializable

class UserModel(db.Model, Serializable):
    __tabelname__ = "User"
    id = db.Column(db.Integer,primary_key=True,autoincrement=True)
    username = db.Column(db.String(255),nullable=False)
    password = db.Column(db.String(255),nullable=False)
    email = db.Column(db.String(255), nullable=True,unique=True)
    join_time = db.Column(db.DateTime,default=datetime.now)
    reset_time = db.Column(db.DateTime, default=datetime.now)
    is_delete = db.Column(db.Boolean, default=False)
    admin_is_banned = db.Column(db.Boolean, default=False)
    state = db.Column(db.String(255),nullable=False,default="1")
    member_state = db.Column(db.String(255))
    join_member_time = db.Column(db.DateTime,default=datetime(2023, 3, 26, 12, 0))
    def __str__(self):
        return f'{self.id}: {self.username}'
    __repr__ = __str__
    def to_dict(self):
        return {
            'id': self.id,
            'username': self.username,
            'admin_is_banned': int(self.admin_is_banned),
            'join_time': self.join_time.strftime('%Y-%m-%d %H:%M:%S'),  # 格式化日期时间
            'reset_time':self.reset_time.strftime('%Y-%m-%d %H:%M:%S')
        }
class Access_Token(db.Model,Serializable):
    __tabelname__ = "Access_Token"
    id = db.Column(db.Integer, primary_key=True)
    is_delete = db.Column(db.Boolean, default=False)
    access_token = db.Column(db.String(500))
    user_id = db.Column(db.Integer)
class IncomeModel(db.Model,Serializable):
    __tabelname__ = "Income"
    id = db.Column(db.Integer, primary_key=True)
    amount = db.Column(db.Float, nullable=False)  # 收入金额
    is_delete = db.Column(db.Boolean,default=False)
    join_time = db.Column(db.DateTime,default=datetime.now)
class EmailCaptchaaModel(db.Model,Serializable):
    __tabelname__ = "email_captcah"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    email = db.Column(db.String(255), nullable=False)
    captcha = db.Column(db.String(255),nullable=False)
    join_time = db.Column(db.DateTime, default=datetime.now)
    is_delete = db.Column(db.Boolean,default=False)

class CouponCode(db.Model,Serializable):
    __tabelname__ = 'coupon_code'
    id = db.Column(db.Integer,primary_key=True,autoincrement=True)
    code = db.Column(db.String(255),nullable=False)
    join_time = db.Column(db.DateTime, default=datetime.now)
    expiry_date = db.Column(db.DateTime,nullable=False)
    duration = db.Column(db.Integer,nullable=False)
    is_delete = db.Column(db.Boolean,default=False)
class TestReport(db.Model, Serializable):
    __tablename__ = 'test_report'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    username = db.Column(db.String(255), nullable=False)
    url = db.Column(db.String(255), default='https:159723.pdf')
    is_delete = db.Column(db.Boolean, default=False)
    # 定义与TestRecord的关系
    test_records = relationship('TestRecord', backref='test_report')

class TestRecord(db.Model, Serializable):
    __tablename__ = 'test_record'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    username = db.Column(db.String(255), nullable=False)
    member_state = db.Column(db.String(255))
    # 设置外键
    TestReport_id = db.Column(db.Integer, db.ForeignKey('test_report.id'))
    url = db.Column(db.String(255), default='https:159723.pdf')
    checkPerson = db.Column(db.String(255))
    state = db.Column(db.String(255), default=False)
    join_time = db.Column(db.DateTime, default=datetime.now)
    report_name = db.Column(db.String(255))
    is_delete = db.Column(db.Boolean, default=False)

    def to_dict(self):
        return {
            'id': self.id,
            'username': self.username,
            'member_state': self.member_state,
            'url': self.url,
            'checkPerson': self.checkPerson,
            'state': self.state,
            'join_time': self.join_time.strftime('%Y-%m-%d %H:%M:%S')  # 格式化日期时间
        }





